﻿using System;
using System.Collections.Specialized;
using System.Configuration;
using System.Text.RegularExpressions;

namespace Baby.VideoConvertor
{
    public static class Setting
    {
        private static readonly string _logoName;
        private static readonly string _videoSize;
        private static readonly string _videoBitrate;
        private static readonly string _videoQuality;
        private static readonly string _audioBitrate;
        private static readonly string _audioChannels;
        private static readonly string _audioSamplingFrequency;
        private static readonly string _trashDirectory;
        private static readonly string _enabledEmailNotice;
        private static readonly string _toEmail;
        private static readonly string _cropScreen;

        static Setting()
        {
            _logoName = AppSettings["LogoName"];
            _videoSize = AppSettings["VideoSize"];
            _videoBitrate = AppSettings["VideoBitrate"];
            _videoQuality = AppSettings["VideoQuality"];
            _audioBitrate = AppSettings["AudioBitrate"];
            _audioChannels = AppSettings["AudioChannels"];
            _audioSamplingFrequency = AppSettings["AudioSamplingFrequency"];
            _trashDirectory = AppSettings["TrashDirectory"];
            _enabledEmailNotice = AppSettings["EnabledEmailNotice"];
            _cropScreen = AppSettings["CropScreen"];
            _toEmail = AppSettings["ToEmail"];
        }

        private static NameValueCollection AppSettings { get { return ConfigurationManager.AppSettings; } }

        /// <summary>
        /// Logo文件名
        /// </summary>
        public static string LogoName
        {
            get { return _logoName; }
        }

        /// <summary>
        /// 视频尺寸
        /// </summary>
        public static string VideoSize
        {
            get { return _videoSize; }
        }

        /// <summary>
        /// 视频码率
        /// </summary>
        public static string VideoBitrate
        {
            get { return _videoBitrate; }
        }

        /// <summary>
        /// 视频质量
        /// </summary>
        public static string VideoQuality
        {
            get { return _videoQuality; }
        }

        /// <summary>
        /// 音频码率
        /// </summary>
        public static string AudioBitrate
        {
            get { return _audioBitrate; }
        }

        /// <summary>
        /// 音频声道
        /// </summary>
        public static string AudioChannels
        {
            get { return _audioChannels; }
        }

        /// <summary>
        /// 音频采样频率
        /// </summary>
        public static string AudioSamplingFrequency
        {
            get { return _audioSamplingFrequency; }
        }

        /// <summary>
        /// 转换失败文件存放目录
        /// </summary>
        public static string TrashDirectory
        {
            get { return _trashDirectory; }
        }

        /// <summary>
        /// 是否启用邮件通知
        /// </summary>
        public static bool EnabledEmailNotice
        {
            get
            {
                if (string.IsNullOrEmpty(_enabledEmailNotice))
                {
                    return false;
                }
                return string.Equals(_enabledEmailNotice, "true", StringComparison.CurrentCultureIgnoreCase);
            }
        }

        /// <summary>
        /// 收件人邮箱 
        /// </summary>
        public static string ToEmail
        {
            get { return _toEmail; }
        }

        /// <summary>
        /// 启用截取视频
        /// </summary>
        public static int CropPosition
        {
            get
            {
                if (string.IsNullOrEmpty(_cropScreen))
                {
                    return 0;
                }
                int t;
                var temp = _cropScreen.Split(':')[0];
                int.TryParse(temp, out t);
                return t;
            }
        }

        /// <summary>
        /// 缩略图片尺寸
        /// </summary>
        public static string ImgSize
        {
            get
            {
                if (string.IsNullOrEmpty(_cropScreen))
                {
                    return "350x240";
                }
                var temp = _cropScreen.Split(new[] { ':' }, StringSplitOptions.RemoveEmptyEntries);
                var pattern = new Regex("[\\d]+x[\\d]+");
                if (temp.Length > 1 && pattern.IsMatch(temp[1]))
                {
                    return temp[1];
                }
                return "350x240";
            }
        }
    }
}